*******************************************************************************
* Load polls data
*******************************************************************************

use dataset_polls, clear

*******************************************************************************
* Keep only polls where reasons asked
*******************************************************************************

drop if mi(fraudc) & mi(fraudflc) & mi(electoralcollegec) & mi(hackingc)

*******************************************************************************
* Get the micro-data
*******************************************************************************

local i = 1
levelsof date_house, local(levels)
foreach l of local levels {
	if `i' == 1 use "polls/`l'", clear
	if `i' >= 2 append using "polls/`l'"
	local i = `i' + 1
}

split date_house, parse("-")
gen date = date(date_house1 + date_house2 + date_house3, "YMD")

*******************************************************************************
* Drop if no microdata
*******************************************************************************

egen nsize = count(legitimatec), by(date_house)
keep if nsize > 10

*******************************************************************************
* Get the correlations for each reason/sample/party
*******************************************************************************

foreach subsample in Democrat Republican {

	foreach x in fraudc fraudflc electoralcollegec hackingc {

		gen cor_`x'`subsample' = .

		levelsof date_house, local(levels)
		foreach l of local levels {
			capture cor `x' legitimatec if date_house == "`l'" ///
				& party == "`subsample'"
			replace cor_`x'`subsample' = r(rho) if ///
				date_house == "`l'" 
		}

	}

}

*******************************************************************************
* Collapse to poll-subsample level
*******************************************************************************

collapse cor*, by(date_house date)

*******************************************************************************
* > Loop over reasons
*******************************************************************************

foreach reason in Fraud ElectoralCollege Hacking {

	preserve
	
		local myreason = lower("`reason'")

		drop if mi(cor_`myreason'cDem) & mi(cor_`myreason'cRep)

		#delimit;
		local myscatters
			(sc cor_`myreason'cDemocrat date, msym(Oh) col(black))
			(sc cor_`myreason'cRepublican date, msym(O) col(black))
			;
		#delimit cr
		
		local mytext1
		local mytext2

		if "`reason'" == "Fraud" {

			#delimit;
			local myscatters	
			(sc cor_fraudcDemocrat date, msym(Oh) col(black))
			(sc cor_fraudflcDemocrat date, msym(th) col(black))
			(sc cor_fraudcRepublican date, msym(O) col(black))
			(sc cor_fraudflcRepublican date, msym(t) col(black))
			;
			#delimit cr
			
			local mytext1 text(0.4 15706 "Dem")
			local mytext2 text(0.1 15706 "Rep")
		
		}

*******************************************************************************
* Plot
*******************************************************************************

		format date %d

		#delimit;

		gr tw
			`myscatters'
			,
				legend(off)
				plotregion(style(none))
				ylab(, angle(horiz) labcol(black))
				xlab(, labsize(small) labcol(black))
				title(`reason', col(black))
				name(g`reason', replace)
				xtitle("")
				xscale(lcol(black))
				yscale(lcol(black))
				;
			
		#delimit cr
		
	restore

*******************************************************************************
* > Close loop over reasons
*******************************************************************************

}

*******************************************************************************
* Combine plots
*******************************************************************************

gr combine gFraud gElectoralCollege gHacking, rows(3) ///
	xsize(3) ysize(3.5) ycommon

*******************************************************************************
* Save
*******************************************************************************

gr export "_output/figure_appendix_reasons_cor.pdf", replace
	
*******************************************************************************
* End
*******************************************************************************
